/*
 * 05四则运算.c
 *
 *  Created on: 2024年3月17日
 *      Author: HP
 */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

enum opt {
	add, sub, mlt, dive
};

int main1005(){
	srand((size_t)time(NULL));
	FILE* fp = fopen("D:/zsl/四则运算.txt", "w");
	if(!fp){
		return -1;
	}
	int a,b;
	char c;
	char* p = (char*)malloc(sizeof(char)*20);
	for(int i=0; i<100; i++){
		a = rand()%10+1;
		b = rand()%10+1;
		switch(rand()%4){
		case add: c='+'; break;
		case sub: c='-'; break;
		case mlt: c='*'; break;
		case dive: c='/'; break;
		}
		memset(p, 0, 20);
		sprintf(p, "%d%c%d=\n", a, c, b);
		fputs(p, fp);
	}
	free(p);
	fclose(fp);
	p=NULL;
	fp=NULL;
	return 0;
}

int main100501(){
	FILE* fp1 = fopen("D:/zsl/四则运算.txt", "r");
	FILE* fp2 = fopen("D:/zsl/四则运算结果.txt", "w");
	if(!fp1 || !fp2){
		return -1;
	}
	// !feof(fp); EOF -1

	int a, b, sum;
	char c;
	char* p = (char*)malloc(sizeof(char)*20);
	for(int i=0; i<100; i++){
		memset(p, 0, 20);
		fgets(p, 20, fp1);
		// 6*6=\n
		sscanf(p, "%d%c%d=\n", &a, &c, &b);
		switch(c){
		case '+':sum=a+b; break;
		case '-':sum=a-b; break;
		case '*':sum=a*b; break;
		case '/':sum=a/b; break;
		}
		memset(p, 0, 20);
		sprintf(p, "%d%c%d=%d\n", a, c, b, sum);
		fputs(p, fp2);
	}
	free(p);
	fclose(fp1);
	fclose(fp2);
	p=NULL;
	fp1=NULL;
	fp2=NULL;
	return 0;
}
